﻿namespace Leetcode.N0868;

public class Solution
{
    public int BinaryGap(int n)
    {
        var count = -1;
        var maxCount = 0;
        while (n > 0)
        {
            if (n % 2 == 1)
            {
                if (count >= 0)
                {
                    count++;
                    maxCount = Math.Max(maxCount, count);
                }
                count = 0;
            }
            else if (count >= 0)
            {
                count++;
            }
            n = n >> 1;
        }
        return maxCount;
    }
}